package com.chinasofti.optional;

import java.util.Optional;

public class Demo01 {
    public static void main(String[] args) {
        //method1();
        Student s = null;
        //public static <T> Optional<T> ofNullable(T value)
        Optional<Student> optional = Optional.ofNullable(s);
        //ofNullable方法可以封装null  或者非null
        System.out.println("optional = " + optional);//optional = Optional.empty

    }

    private static void method1() {
        //Student s = new Student();
        //s.setName("lilei");
        //s.setAge(18);
        Student s = null;
        //public static <T> Optional<T> of(T value)
        Optional<Student> optionall = Optional.of(s);
        //optional 可以看做一个容器，里面装了一个引用类型数据对象
        //返回值是optional对象
        //如果我们在of方法中添加了null对象，还是会抛出空指针异常
        System.out.println("optionall = " + optionall);
    }

}
